package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.CourseShareJournalExample;
import com.health.db.model.CourseShareJournalTemp;
import com.health.db.model.CourseShareLedger;
import com.health.db.model.UserAccountJournalTemp;
import com.health.db.service.user.ICourseShareJournalService;
import com.health.db.service.user.ICourseShareLedgerService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryCourseShareBillReq;
import com.health.mapp.bean.QueryCourseShareBillResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 *  课程分享账单查询
 * @author yuchong
 *
 */
public class QueryCourseShareBillImpl implements ActionInterface {
	
	@Autowired
	private ICourseShareJournalService courseShareJournalService ;
	@Autowired
	private ICourseShareLedgerService courseShareLedgerService ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryCourseShareBillReq req=(QueryCourseShareBillReq) reqBean;
		QueryCourseShareBillResp _resp= new QueryCourseShareBillResp();
		
		CourseShareJournalExample temp = new CourseShareJournalExample();
		Integer pageNo=req.getPageNo();
		Integer pageSize=req.getPageNum();
		if(null!=pageNo&&null!=pageSize){
			temp.setOffset((pageNo.intValue()-1)*pageSize.intValue());
			temp.setPageSize(pageSize.intValue());
		}
		
		if (req.getUserId()==0) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}else{
			temp.setUserId(req.getUserId());
		}
		
		List<UserAccountJournalTemp> billList = courseShareJournalService.selectCourseShareJournalSimple(temp);
		CourseShareLedger courShareLedge = courseShareLedgerService.selectByUserId(req.getUserId());
		
		if (null!=courShareLedge&&null!=courShareLedge.getBalanceTotal()) {
			_resp.setBalance(String.valueOf(courShareLedge.getBalanceTotal().setScale(2,  BigDecimal.ROUND_HALF_UP)));
		}
		
		//查询bill列表日志
		List<CourseShareJournalTemp> billListreturn = new ArrayList<CourseShareJournalTemp>();
		
		for (int i = 0; i < billList.size(); i++) {
			UserAccountJournalTemp accounttemp =  billList.get(i) ;
			
			CourseShareJournalTemp uajtemp = new CourseShareJournalTemp();
			
			BeanUtils.copyProperties(accounttemp,uajtemp);
			
			Double total = Double.valueOf(uajtemp.getBillAmount());
			
			if (total>0) {
				String enterAccount = "+"+uajtemp.getBillAmount();
				uajtemp.setBillAmount(enterAccount);
			}
			uajtemp.setBillTime(uajtemp.getBillTime().replace("T", " "));
			billListreturn.add(uajtemp);
		}
		//新
		 _resp.setBillList(billListreturn);
		_resp.setPageNo(pageNo);
		_resp.setPageNum(pageSize);
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
		
		_resp.setGetCashTips("金额超过20元可提现。");
		//_resp.setGetCashTips("金额超过20元可提现，今天还剩【1】次");
 
		return _resp;
	}

	public ICourseShareJournalService getCourseShareJournalService() {
		return courseShareJournalService;
	}

	public void setCourseShareJournalService(
			ICourseShareJournalService courseShareJournalService) {
		this.courseShareJournalService = courseShareJournalService;
	}

	public ICourseShareLedgerService getCourseShareLedgerService() {
		return courseShareLedgerService;
	}

	public void setCourseShareLedgerService(
			ICourseShareLedgerService courseShareLedgerService) {
		this.courseShareLedgerService = courseShareLedgerService;
	}
	
}
